package com.yuqing.sentimentapp.mine.presenter.impl;

import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.mine.MineFragment;
import com.yuqing.sentimentapp.mine.bean.MinePersonInfoBean;
import com.yuqing.sentimentapp.mine.presenter.MinePersonInfoPresenter;
import com.yuqing.sentimentapp.module.ModuleManager;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MinePersonInfoPresenterImpl implements MinePersonInfoPresenter {

    private MineFragment mineFragment;

    public MinePersonInfoPresenterImpl(MineFragment mineFragment) {
        this.mineFragment = mineFragment;
    }

    @Override
    public void loadPersonCenterInfo(String mobile) {
        ModuleManager.getInstance().loadPersonCenterResult(mineFragment.getContext(), mobile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MinePersonInfoBean>() {
                    @Override
                    protected void hideDialog() {
                        mineFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        mineFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(MinePersonInfoBean minePersonInfoBean) {
                        if (checkContext()) return;
                        mineFragment.onSuccess(minePersonInfoBean);
                    }

                    @Override
                    protected void onFailure(String message) {
                        mineFragment.onFailure(message);
                    }

                });
    }

    private boolean checkContext() {
        if (mineFragment.getActivity() == null || mineFragment.getActivity().isFinishing() || mineFragment.getActivity().isDestroyed())
            return true;
        return false;
    }
}
